﻿using System;
using System.IO;
using System.Linq;
class FileProcess
{
    //存取文件地址
    internal static Tuple<string, string, string> GetFile()
    {
        string input = @"C:\Users\leomojys\Desktop\input.txt";
        string output = @"C:\Users\leomojys\Desktop\output.txt";
        string word = @"C:\Users\leomojys\Desktop\words.txt";
        return Tuple.Create(input, output, word);
    }
    //处理文件
    internal static void ProcessFile(string input, string output, string word)
    {
        try
        {
            List<string> Lines = new List<string>();

            //读取敏感词
            Dictionary<string, string> sensitiveWords = SensitiveWordFilter.SensitiveWords(word);

            // 逐行读取文件内容
            using (StreamReader reader = new StreamReader(input))
            {
                using (StreamWriter writer = new StreamWriter(output))
                {
                    string line = reader.ReadLine();
                    string nextLine = reader.ReadLine(); 
                    while (line.Length != 0)
                    {
                        // 处理当前行
                        (string thisLine,int position) = SensitiveWordFilter.FilterSensitiveWords(line, sensitiveWords);

                        if(position != 0)
                        {
                            writer.WriteLine(thisLine);
                            Console.WriteLine(thisLine);//命令窗口显示替换内容
                        }
                        // 处理当前行与下一行可能的关联（关键字跨越两行）
                        nextLine = line.Remove(0, position) + nextLine; 
                        
                        line = nextLine;  
                        nextLine = reader.ReadLine();
                        
                        if(nextLine == null)
                        {
                            (thisLine, position) = SensitiveWordFilter.FilterSensitiveWords(line, sensitiveWords);
                            while (position != 0)
                            {
                                writer.WriteLine(thisLine);
                                Console.WriteLine(thisLine);//命令窗口显示替换内容
                                nextLine = line.Remove(0, position);
                                line = nextLine;
                                (thisLine, position) = SensitiveWordFilter.FilterSensitiveWords(line, sensitiveWords);
                                nextLine = line.Remove(0, position);
                            }
                            writer.WriteLine(nextLine);
                            Console.WriteLine(nextLine);//命令窗口显示替换内容
                            break;
                        }
                    }  
                }
            }
        }
        catch (System.Exception ex)  //捕获异常
        {
            Console.WriteLine("Exception catch here - details  : " + ex.ToString());
        }
    }
}

class SensitiveWordFilter
{
    // 敏感词字典
    internal static Dictionary<string, string> SensitiveWords(string word)
    {
        Dictionary<string, string> sensitiveWords = new Dictionary<string, string>();

        string[] lines = File.ReadAllLines(word);//将每一行存储为字符串数组lines中的一个元素

        foreach (string line in lines)
        {
            string[] parts = line.Split(',');
            if (parts.Length == 2)
            {
                sensitiveWords.Add(parts[0], parts[1]);//将敏感词和替换词添加到字典中
            }
        }
        return sensitiveWords;
    }

    // 敏感词处理方法
    internal static Tuple<string, int> FilterSensitiveWords(string line, Dictionary<string, string> sensitiveWords)
    {
        int replacementPosition = 0;
        int findIndex = 0;
        List<int> findIndexs = new List<int>();
        List<int> replacementPositions = new List<int>();

        foreach (var word in sensitiveWords)
        {
            string pattern = word.Key;
            string replacement = word.Value;
            int index = 0;

            while ((index = line.IndexOf(pattern, index)) != -1)
            {
                findIndex = index + pattern.Length;
                replacementPosition = index + replacement.Length;
                
                findIndexs.Add(findIndex);
                replacementPositions.Add(replacementPosition);
                break;
            }
        }

        foreach (var word in sensitiveWords)
        {
            string pattern = word.Key;
            string replacement = word.Value;

            line = System.Text.RegularExpressions.Regex.Replace(line, pattern, replacement);
        }

        if (findIndexs.Count != 0)
        {
            findIndex = findIndexs.Min();
            replacementPosition = replacementPositions.Min();
            
            string ThisLine = line.Substring(0,replacementPosition);
            return Tuple.Create(ThisLine, findIndex);
        }
        return Tuple.Create(line, findIndex);
    }
}
public class Program
{
    static void Main(string[] args)
    {
        var filePaths = FileProcess.GetFile();
        // 从元组中提取文件路径
        string inputFilePath = filePaths.Item1;
        string outputFilePath = filePaths.Item2;
        string wordsFilePath = filePaths.Item3;

        FileProcess.ProcessFile(inputFilePath, outputFilePath, wordsFilePath);
    }
}